import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;

/**
 * Created with IntelliJ IDEA.
 * User: Thoughtworker
 * Date: 8/25/13
 * Time: 2:19 PM
 * To change this template use File | Settings | File Templates.
 */
public class TennisGame {
    private PrintStream printStream;
    private BufferedReader bufferedReader;
    Score playerOneScore = Score.Love;
    Score playerTwoScore = Score.Love;

    public TennisGame(PrintStream printStream, BufferedReader bufferedReader) {
        this.printStream = printStream;
        this.bufferedReader = bufferedReader;
    }

    public void run() throws IOException {
        while((playerOneScore != Score.Win) && (playerTwoScore != Score.Win)){
            printStream.println("Who won the point?");
            scorePoint(Integer.parseInt(bufferedReader.readLine()));
        }
    }

    public void printScore() {
        if(playerOneScore == Score.Forty && playerTwoScore == Score.Forty) {
            printStream.println("Deuce");
        } else {
            printStream.println(playerOneScore.toString(playerTwoScore) + " - " + playerTwoScore.toString(playerOneScore));
        }
    }

    public void scorePoint(int player) {
        if(player == 1) {
            playerOneScore = playerOneScore.scorePoint(playerTwoScore);
            playerTwoScore = playerTwoScore.losePoint(playerOneScore);
        } if(player == 2) {
            playerOneScore = playerOneScore.losePoint(playerTwoScore);
            playerTwoScore = playerTwoScore.scorePoint(playerTwoScore);
        }
        printScore();
    }
}
